home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Full / NetObjects Fusion 9 Standard / NOF9_Full_EN.exe / data1.cab / FSI / lib / nof / NOF.js < prev    next >
Encoding:
JavaScript  |  2005-11-16  |  10.4 KB  |  407 lines

  1. /****i* SOURCE_FILE/INFO
  2. *
  3. * NAME
  4. *    NOF.js
  5. *
  6. * USAGE
  7. *    Part of WPS JavaScript Library.
  8. *
  9. * COPYRIGHT
  10. *    Copyright ⌐ 2000-2001 Website Pros, Inc.
  11. *    All Rights Reserved.
  12. *
  13. *  This is an unpublished work protected by Website Pros, Inc.
  14. *  as a trade secret, and is not to be used or disclosed except as
  15. *  expressly provided in a written license agreement executed by
  16. *  you and Website Pros, Inc.
  17. *
  18. *      <copyright@websitepros.com>
  19. *
  20. * NOTES
  21. *    JavaScript code.
  22. *
  23. *****/
  24.  
  25. /****h* NOF_JavaScript_Library/NOF
  26.     *
  27.     * NAME
  28.     *    NOF JavaScript Library / main module.
  29.     *
  30.     * DESCRIPTION
  31.     *    Definition of NOF "namespace".
  32.     *
  33.     * NOTES
  34.     *    (*) To place another name under this namespace use:
  35.     *        NOF.prototype.<your-name> = <whatever>;
  36.     *
  37.     ****/
  38.  
  39. function IS_getLibHandle()
  40. {
  41.   return ((typeof(top) != "undefined" && typeof(top._is) != "undefined") ? top._is :  // browsers
  42.          ((typeof(top) != "undefined" && typeof(top.opener) != "undefined" && top.opener != null && typeof(top.opener._is) != "undefined") ? top.opener._is :  // browsers  
  43.          ((typeof(MM)  != "undefined" && typeof(MM._is) != "undefined") ? MM._is :    // MacroMedia DW
  44.          ((typeof(_is) != "undefined") ? _is : null))));                              // ??
  45. }
  46.  
  47. var IS = IS_getLibHandle();
  48. if (!IS_isModuleInitialized("IS.NOF"))
  49. {
  50.   /****m* NOF/addVariable
  51.    *
  52.    * NAME
  53.    *    addVariable( name, initValue )
  54.    *
  55.    * USAGE
  56.    *    name      -- variable name
  57.    *    initValue -- initial value
  58.    *
  59.    * DESCRIPTION
  60.    *    Adds a new variable, 'name', to the NOF namespace. If 'initValue' is specified
  61.    *    the new variable will be initialized with this value.
  62.    *
  63.    * RETURN VALUE
  64.    *    none
  65.    *
  66.    ****/
  67.   function NOF_addVariable(name, initValue) {
  68.     if (arguments.length == 0) { return; }
  69.     if (arguments.length == 1) { initValue = null; }
  70.     eval("NOF." + name + " = initValue");
  71.   }
  72.  
  73.  
  74.   /****m* NOF/delVariable
  75.    *
  76.    * NAME
  77.    *    delVariable( name )
  78.    *
  79.    * USAGE
  80.    *    name -- variable name
  81.    *
  82.    * DESCRIPTION
  83.    *    Removes a variable, 'name', from the NOF namespace
  84.    *
  85.    * RETURN VALUE
  86.    *    none
  87.    *
  88.    ****/
  89.   function NOF_delVariable (name) {
  90.     if (arguments.length == 0) { return; }        
  91.     if (typeof(eval("NOF." + name)) != "undefined") {
  92.       eval("delete NOF." + name);
  93.     }
  94.   }
  95.  
  96.     /****m* NOF/loadClasses
  97.         *
  98.         * NAME
  99.         *    loadClasses( pathNOF, ...)
  100.         *
  101.         * USAGE
  102.         *    pathNOF -- comune path to classes (relative to NetObjects System folder
  103.         *  the rest of the parameters are the rest of the path to clasess
  104.         *
  105.         * DESCRIPTION
  106.         *  load one ore more JS classes (the objects bound to NOF will persist and 
  107.         *  will be accesible from the momment of loading). Usage sample:
  108.         *    NOF.loadClasses(NOF.App.getSystemDirectory() + "FSI/",
  109.         *            "lib/nof/dialogs/MySpecialFileDlg",
  110.         *            "PhotoGallery/lib/PhotoGallery");
  111.         * MySpecialFileDlg.js and PhotoGallery.js will be loaded
  112.         *
  113.         * RETURN VALUE
  114.         *    none
  115.         *
  116.         ****/    
  117.     function NOF_loadClasses( pathNOF ) {
  118.         for (var i=1; i < arguments.length; i++) {
  119.             var filePath = pathNOF + arguments[i];
  120.             var fileExtension = filePath.substring(filePath.lastIndexOf(".") + 1);
  121.             if (fileExtension.toLowerCase() != "js") {
  122.                 filePath += ".js";
  123.             }
  124.             //alert("filePath = " + filePath); 
  125.             if (typeof(NOF.IO.File) == "undefined") {
  126.                 /*throw new NOF.UTIL.Exception();*/ 
  127.                 return null;
  128.             }
  129.             var file = new NOF.IO.File(filePath);
  130.             if (!file.exists()) {
  131.                 continue;
  132.             }
  133.             var content    = file.read();            
  134.             if (content != null) { 
  135.                 try{
  136.                     eval(content);
  137.                 } catch( exc ) {
  138.                     //TODO: ? throw something ?
  139.                     var errStr = "Message: " + exc.message + "\n" +
  140.                         "File: " + exc.fileName + "\n" +
  141.                         "Line: " + exc.lineNumber + "\n" +
  142.                         "Name: " + exc.name;                    
  143.                     alert(errStr);
  144.                     //return null;
  145.                     continue;
  146.                 }
  147.             }
  148.         }        
  149.     }
  150.  
  151.     /****m* NOF/ensureDependencies
  152.     *
  153.     * NAME
  154.     *    ensureDependencies(String[] completeClassNames)
  155.     *
  156.     * USAGE
  157.     *    completeClassNames - the list of classes to be checked if initialized.
  158.     * The class name must be given with the complete namespace. The path  
  159.     * to the js file (excluding the extension) containg the class can also be used.
  160.     *
  161.     * DESCRIPTION
  162.     *  ensure that the classes passed as arguments are loaded
  163.     * (intialized and bound to the correct namespace).
  164.     * If they are not already loaded, the method tries to load them dinamically.
  165.     * Usage sample:
  166.     *    NOF.ensureDependencies("NOF.IO.TextElementWriter", "nof/net/Http", "MYNS.DHTML.SpecialEffects");
  167.     * 
  168.     *
  169.     * RETURN VALUE
  170.     *    none
  171.     *
  172.     ****/    
  173.     function NOF_ensureDependencies( /*String[]*/ classNames ) {
  174.         if (typeof(classNames) != "array") {
  175.             if (typeof(classNames) == "string") {
  176.                 classNames = [classNames];
  177.             } else {
  178.                 return;
  179.             }
  180.         }
  181.         
  182.         var scriptsDirectory = NOF.App.getScriptsDirectory() + NOF.App.PATH_SEPARATOR;
  183.         
  184.         for (var i=0; i < classNames.length; i++) {
  185.             var className = classNames[i];
  186.             var classPath = "";
  187.             if ((className.indexOf("/") == -1) /*&& (className.indexOf("\\") == -1)*/) { 
  188.                 //it is a NS style name
  189.                 classPath = className.replaceSubstr(".", "/"); //NOF.App.PATH_SEPARATOR == /
  190.             } else {
  191.                 //it is a file path
  192.                 classPath = className;
  193.                 className = className.replaceSubstr("/", "."); //NOF.App.PATH_SEPARATOR == /
  194.                 // we should do an uppercase here!
  195.             }
  196.             classPath = scriptsDirectory + classPath;
  197.             if (IS_isModuleInitialized("IS." + className)) {
  198.                 continue;
  199.             } else {
  200.                 try{
  201.                     NOF.loadClasses("", classPath);
  202.                 } catch( exc ) {
  203.                     //TODO: ? throw something ?
  204.                     var errStr = "Message: " + exc.message + "\n" +
  205.                         "File: " + exc.fileName + "\n" +
  206.                         "Line: " + exc.lineNumber + "\n" +
  207.                         "Name: " + exc.name;                    
  208.                     alert(errStr);
  209.                     //return null;
  210.                     continue;
  211.                 }                
  212.             }            
  213.  
  214.         }
  215.     }
  216.     
  217.     
  218.   /****c* NOF
  219.   **
  220.   ** NAME
  221.   **  NOF()
  222.   **
  223.   ** USAGE
  224.   **
  225.   ** DESCRIPTION
  226.   **  Namespace for the Website Pros JS library.
  227.   **
  228.   */
  229.   function NOF () {
  230.     // __proto__ initialization
  231.     this.__proto__ = NOF.prototype;
  232.               
  233.     // properties
  234.     this.type = IS.TYPE_NAMESPACE;
  235.     
  236.     this.addVariable = NOF_addVariable;
  237.     this.delVariable = NOF_delVariable;
  238.         this.loadClasses = NOF_loadClasses;
  239.         this.ensureDependencies = NOF_ensureDependencies;
  240.   }
  241.     
  242.   // add NOF namespace to IS namespace
  243.   IS.__proto__.NOF = new NOF();
  244. }
  245.  
  246. var NOF = IS.NOF;
  247.  
  248. Function.prototype.inherits = function Function_inherits( parent ) {
  249.  
  250.     this.prototype = new parent();    
  251.     this.prototype.__super__ = parent;
  252.  
  253. /*    
  254.     //this.prototype = new parent();
  255.     for (baseProp in parent.prototype)
  256.         this.prototype[baseProp] = parent.prototype[baseProp];  
  257.         
  258.     this.prototype.__super__ = parent;    
  259. */
  260. };  
  261.  
  262. Object.prototype.SUPER = function Object_SUPER(){
  263.   this.__proto__.__super__.apply(this, Array.prototype.slice.apply(arguments, [0]));
  264. }
  265.  
  266. Object.prototype.release = function Object_release(){
  267.   for (var i in this){
  268.   if (typeof(this[i]) == 'object' && this[i] != null && typeof(this[i].release) == 'function'){
  269.     /*
  270.     if (this[i].toString )
  271.       NOF.SysOut.info("releasing: " + i + "=" + this[i].toString() );
  272.     else
  273.       NOF.SysOut.info("releasing: " + i + "=" + this[i] );
  274.     */
  275.     this[i].release(); //the order being unpredictable could cause problems
  276.   }
  277.  
  278.   if (typeof(this[i]) != 'function' || (this[i] != 'release' && this[i] != 'RELEASE'))
  279.       this[i] = null;
  280.   }
  281.  
  282.   if (this.prototype != null){
  283.     if (typeof(this.prototype.release) == 'function')
  284.       this.prototype.release();
  285.     
  286.     this.prototype.__super__ = null;
  287.   }
  288.   
  289.   this.__proto__ = null;
  290.   this.prototype = null;
  291. }
  292.  
  293. Object.prototype.RELEASE = Object_release;
  294.   
  295. Object.prototype.dump = function Object_dump( showFunctions, obj_name ){
  296.   var prefix = (obj_name == null || obj_name.length == 0 ? "" : obj_name + "." );
  297.   showFunctions = (showFunctions != null ? showFunctions : false); 
  298.   
  299.   var result = "";
  300.   for (var i in this)
  301.     if (typeof(this[i]) != 'function' || showFunctions)
  302.       result += prefix + i + " = " + this[i] + "\n";
  303.     
  304.   return result + "\n\n";
  305. }  
  306.   
  307. Array.prototype.toArray = function () {
  308.     var cloneArray = new Array();
  309.     for (var i = 0; i < this.length; i++) {
  310.         cloneArray.add(this[i]);
  311.     }
  312.     //return this; //?
  313. }
  314.  
  315. // Inserts the specified element at the specified position in this array;
  316. // If the index is not specified then the element is inserted at the end of the array.
  317. Array.prototype.add = function ( /*Object*/ obj, /*int*/ i) {
  318.  
  319.     if (i != null) {        
  320.         if ( (i < 0) || (i > this.length)) {
  321.             return;
  322.         }
  323.         if (i == this.length) {
  324.             this.add(obj);
  325.             return;
  326.         }
  327.         for (var j = this.length - 1; j >= i; j--) {
  328.             this[j+1] = this[j];
  329.         }
  330.         //this[i] = obj;
  331.     } else {
  332.         //this[this.length] = obj;
  333.         i = this.length;
  334.     }
  335.     this[i] = obj;
  336. }    
  337.     
  338. Array.prototype.item = function (i) {
  339.     return this[i];
  340. }
  341.     
  342. Array.prototype.removeItem = function(/*int*/ i) {
  343.   
  344.   if ( i < 0 || i >= this.length){
  345.     return;
  346.   }   
  347.   
  348.   for ( var j = i; j < this.length - 1; j++ ) {
  349.     this[j] = this[j+1];
  350.   }
  351.   this[this.length - 1] = null;
  352.   this.length--;
  353.   return;
  354. }
  355. Array.prototype.remove = Array.prototype.removeItem;
  356.     
  357. Array.prototype.containsItem = function(item){
  358.   
  359.   var index = -1;
  360.   var hasEquals = (item.equals)?true:false; //typeof(item.equals)=="function"
  361.   
  362.   if(hasEquals){
  363.     for ( var j = 0; j < this.length; j++ ) {
  364.       if( item.equals(this[j]) ) {
  365.         index = j;
  366.         break;
  367.       }   
  368.     }
  369.   }else{
  370.     for ( var j = 0; j < this.length; j++ ) {
  371.       if( item == this[j] ) {
  372.         index = j;
  373.         break;
  374.       }   
  375.     }   
  376.   }
  377.   return index;
  378. }  
  379. Array.prototype.contains = Array.prototype.containsItem;
  380.  
  381.     
  382. String.prototype.trim = function trimString() {
  383.   // Use a regular expression to replace leading and trailing 
  384.   // spaces with the empty string
  385.   return this.replace(/(^\s*)|(\s*$)/g, "");
  386. }
  387.  
  388. String.prototype.replaceSubstr = function (/*String*/ whatStr, /*String*/ withStr) {
  389.   if ((whatStr == null) || (whatStr.length < 1) ) {
  390.     return this.toString();
  391.   } 
  392.   if (withStr == null) {
  393.     withStr = "";
  394.   }
  395. /*    
  396.     var r, re;
  397.     eval("re = /"+ whatStr + "/gm;"); // !this can be dangerous for various instances of whatStr!
  398.     r = this.replace(re, withStr);
  399.     return(r);    
  400. */
  401.     var strArr = this.split(whatStr);
  402.     var str = strArr.join(withStr);
  403.     return str;           
  404.  
  405.  
  406.